<template>
  <el-dialog :title="info.title" :visible.sync="visible" append-to-body center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="发薪标题" prop="title" style="width: 100%">
            <el-input v-model="form.title" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="发薪月份" prop="salary_date">
            <el-date-picker
              v-model="form.salary_date"
              type="month"
              format="yyyy-MM"
              value-format="yyyy-MM"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button class="fl" size="small" @click="handleCancel()">取 消</el-button>
            <el-upload
              ref="upload"
              class="fl"
              style="margin-left: 10px;height: 32px; line-height: 1"
              :limit="1"
              action="#"
              accept=".xlsx"
              list-type="text"
              :http-request="handleUpload"
              :on-change="fileChange"
              :show-file-list="false"
            >
              <el-button type="primary" size="small">导 入</el-button>
            </el-upload>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import page from '@/utils/page'
import { data } from '@/api/file'

export default {
  extends: page,
  data() {
    return {
      rules: {
        salary_date: [{ required: true, trigger: 'blur', message: '发薪月份不可为空' }],
        title: [{ required: true, trigger: 'blur', message: '发薪标题不可为空' }]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
      }
    }
  },
  mounted() {
  },
  methods: {
    handleUpload(param) {
      this.$refs['formDate'].validate(async(valid) => {
        const formData = new FormData()
        formData.append('title', this.form.title)
        formData.append('salary_date', this.form.salary_date)
        formData.append('file', param.file)
        formData.append('type', 'salary')
        data(formData).then(res => {
          this.getData()
          this.handleCancel()
          this.$message.success('导入成功')
        }).catch(err => {
          this.$message.error(err.msg)
        })
      })
    }
  }
}
</script>

<style>

</style>
